﻿using FreeSql;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImportPlcPointPosition {

    public class FreeSqlHelper {

        public static IFreeSql FreeSql { get; private set; }

        public static IFreeSql CreateInstance() {
            var dbConfigs = AppConfig.AppsettingConfig.DbConfigs;
            if (FreeSql == null) {
                //初始化FreeSql 
                IFreeSql fsql = new FreeSqlBuilder()
                        .UseConnectionString(DataType.MySql, dbConfigs[0].Conn)
                        .UseAdoConnectionPool(true)
                        .UseMonitorCommand(cmd => Debug.WriteLine($"Sql：{cmd.CommandText}"))
                        .UseAutoSyncStructure(true) //自动同步实体结构到数据库，只有CRUD时才会生成表
                        .Build();
                fsql.Aop.ConfigEntityProperty += (s, e) => {
                    if (e.Property.PropertyType == typeof(decimal) || e.Property.PropertyType == typeof(decimal?)) {
                        e.ModifyResult.Precision = 18;
                        e.ModifyResult.Scale = 6;
                    }
                };
                FreeSql = fsql;
            }
            return FreeSql;
        }
    }

}
